package com.ld.collegetask;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alipay.sdk.app.PayTask;
import com.google.gson.Gson;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.beans.MemberBean;
import com.ld.collegetask.databinding.ActivityMemberWalletChargeApplyBinding;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MemberWalletChargeApplyActivity extends BaseActivity {
    private ActivityMemberWalletChargeApplyBinding binding;
    private List<RelativeLayout> list_rl;
    private List<ImageView> list_img;

    private Integer userId = 0;
    private String money = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_member_wallet_charge_apply);

        SharePreference sp = new SharePreference(getBaseContext());
        userId = sp.getUserId();

        setTitleView(this, binding.ilTitle, true, true, false, false);
        setLoadingView(this, binding.ilLoading);
        setTitle("充值");

        findView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loading_go();
    }

    @Override
    public void loading() {
        member_info();
    }

    public void findView() {
        list_rl = new ArrayList<>();
        list_rl.add(binding.option1);
        list_rl.add(binding.option2);
        list_rl.add(binding.option3);
        list_rl.add(binding.option4);
        list_rl.add(binding.option5);
        list_rl.add(binding.option6);

        list_img = new ArrayList<>();
        list_img.add(binding.option1Img);
        list_img.add(binding.option2Img);
        list_img.add(binding.option3Img);
        list_img.add(binding.option4Img);
        list_img.add(binding.option5Img);
        list_img.add(binding.option6Img);

        setReset();
        binding.llAipay.setOnClickListener(this);
        binding.moneyButton.setOnClickListener(this);
    }

    private void setReset() {
        for (int i = 0; i < list_rl.size(); i++) {
            list_rl.get(i).setOnClickListener(this);
            list_rl.get(i).setSelected(false);
        }

        for (int i = 0; i < list_img.size(); i++) {
            list_img.get(i).setVisibility(View.GONE);
        }
    }

    private void member_charge_apply(String amount) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_charge_apply(userId, amount);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                final BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    System.out.println("=========================充值成功");
                    new Thread() {
                        public void run() {
                            try {
                                PayTask alipay = new PayTask(mActivity);
                                Map<String, String> result = alipay.payV2(base.getJson(), true);
                                Intent intent = new Intent(mActivity, AlipayActivity.class);
                                intent.putExtra("result", result.get("result"));
                                intent.putExtra("resultStatus", result.get("resultStatus"));
                                intent.putExtra("memo", result.get("memo"));
                                mActivity.startActivity(intent);
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }.start();
                } else {

                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    private void member_info() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_info(userId);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    MemberBean member = gson.fromJson(base.getJson(), MemberBean.class);
                    binding.setMemberBean(member);
                    loading_end();
                } else {
                    loading_error();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading_error();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.option1:
                setReset();
                list_rl.get(0).setSelected(true);
                list_img.get(0).setVisibility(View.VISIBLE);
                binding.money.setText(binding.option1Text.getText().toString());
                money = binding.option1Text.getText().toString().split("元")[0];
                break;
            case R.id.option2:
                setReset();
                list_rl.get(1).setSelected(true);
                list_img.get(1).setVisibility(View.VISIBLE);
                binding.money.setText(binding.option2Text.getText().toString());
                money = binding.option1Text.getText().toString().split("元")[0];
                break;
            case R.id.option3:
                setReset();
                list_rl.get(2).setSelected(true);
                list_img.get(2).setVisibility(View.VISIBLE);
                binding.money.setText(binding.option3Text.getText().toString());
                money = binding.option1Text.getText().toString().split("元")[0];
                break;
            case R.id.option4:
                setReset();
                list_rl.get(3).setSelected(true);
                list_img.get(3).setVisibility(View.VISIBLE);
                binding.money.setText(binding.option4Text.getText().toString());
                money = binding.option1Text.getText().toString().split("元")[0];
                break;
            case R.id.option5:
                setReset();
                list_rl.get(4).setSelected(true);
                list_img.get(4).setVisibility(View.VISIBLE);
                binding.money.setText(binding.option5Text.getText().toString());
                money = binding.option1Text.getText().toString().split("元")[0];
                break;
            case R.id.option6:
                setReset();
                list_rl.get(5).setSelected(true);
                list_img.get(5).setVisibility(View.VISIBLE);
                binding.money.setText(binding.option6Text.getText().toString());
                money = binding.option1Text.getText().toString().split("元")[0];
                break;
            case R.id.ll_aipay:
                if (binding.option7Img.getVisibility() == View.GONE) {
                    binding.option7Img.setVisibility(View.VISIBLE);
                } else {
                    binding.option7Img.setVisibility(View.GONE);
                }
                break;
            case R.id.money_button:
                if (binding.option7Img.getVisibility() == View.VISIBLE&&!money.equals("0")) {
                    member_charge_apply(money);
                }
                break;
        }
    }
}
